package me.kafeitu.demo.activiti;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.flowable.common.engine.impl.persistence.StrongUuidGenerator;
import org.flowable.common.rest.resolver.DefaultContentTypeResolver;
import org.flowable.dmn.api.DmnDecisionTable;
import org.flowable.dmn.api.DmnDeployment;
import org.flowable.dmn.api.DmnRepositoryService;
import org.flowable.dmn.api.DmnRuleService;
import org.flowable.dmn.engine.*;

import cn.tanghom.app.oa.model.Position;
import cn.tanghom.app.oa.model.Talent;
import cn.tanghom.app.system.model.User;


public class MatchDmn {
	public static void main(String[] args) {

		// 根据默认配置创建引擎的配置实例	
		
		DmnEngineConfiguration config = DmnEngineConfiguration.createDmnEngineConfigurationFromResourceDefault();

		// 创建规则引擎

		//-DmnEngine engine = config.buildDmnEngine();
		
		DmnEngine engine = DmnEngines.getDefaultDmnEngine();
	    DataSource dataSource = engine.getDmnEngineConfiguration().getDataSource();

		// 获取规则的存储服务组件

		DmnRepositoryService rService = engine.getDmnRepositoryService();

		// 获取规则服务组件

		DmnRuleService ruleService = engine.getDmnRuleService();

		// 进行规则 部署
		DmnDeployment dep = rService.createDeployment().addClasspathResource("dmn/通用的岗位规则.dmn").deploy();
		//DmnDeployment dep = rService.createDeployment().addClasspathResource("dmn/match.dmn").deploy();

		// 进行数据查询

		DmnDecisionTable dt = rService.createDecisionTableQuery().deploymentId(dep.getId()).singleResult();

		// 初始化参数

		Map<String, Object> params = new HashMap<String, Object>();
		
		Position position = new Position(2L);		
		position.setAge_required("20-30");
		position.setAnnualSalary(200000);
		position.setCityId(21);
		position.setTitleId("java工程师");
		params.put("titleId", position.getDepName());
		params.put("titleId2", position.getTitleId());
		params.put("position", position);

		Talent resume = new Talent(1L);
		resume.setBirthYear(1990);
		resume.setAnnualSalary(200000);
		resume.setExpect_city("21,11");
		resume.setExpect_position_titles("java工程师,Andorid开发");
		params.put("talent",resume);
		params.put("expectPositions2",resume.getExpect_position_titles());
		// 传入参数执行决策，并返回结果

		Map<String, Object> result = ruleService.createExecuteDecisionBuilder().decisionKey(dt.getKey()).variables(params).executeWithSingleResult();

		// 控制台输出结果

		System.out.println(result.get("matched"));

		// 重新设置参数	
		
		position.setAge_required("20-30");
		position.setAnnualSalary(200000);
		position.setCityId(21);
		
		params.put("titleId2", position.getTitleId());
		params.put("position", position);

		
		resume.setBirthYear(2012);
		resume.setAnnualSalary(100000);
		resume.setExpect_city("21,11");
		
		params.put("talent",resume);

		// 重新执行决策

		Map<String, Object> result2 = ruleService.createExecuteDecisionBuilder().decisionKey(dt.getKey()).variables(params).executeWithSingleResult();

		// 控制台重新输出结果

		System.out.println(result2.get("matched"));

	}
}
